var ctag="0";
$(document).ready(function(){
	//开始预案对话框
	$("#dialog").dialog("destroy");
	/*
	$("#notifyLeaderWindow").dialog({
		autoOpen : false,
		minWidth : 550,
		modal : true,
		title : '选择人员',
		close : function() {
			return false;
		}
	});
	$("#selectUser").click(function() {
		 $("#leaderChooseWindow").dialog("open");
	});*/
	
	

	
	$("#dialog-form").dialog({
		autoOpen: false,
		width: 460,
		modal: true,
		resizable: false,
	    close: function() {
			$("#user-form input").removeClass('invalid');
			$("#user-form label").remove('.invalid');
			
			//test1;
			var myFileArr=window.frames["uploadFrame"].getFileArray();
			$.each(myFileArr,function(i,n){
				var htmlStr="";
				htmlStr+="<div><img src='lzm/images/doc.gif'/><span class='fn'>"
				htmlStr+=n.originName;
				htmlStr+="</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
				htmlStr+="<span class='crtName' style='display:none;'>";
				htmlStr+=n.crtName;
				htmlStr+="</span><span class='dell'>删除</span></div>";
				$('#paListDIV').append(htmlStr);
			});
			$('#planb').val(goodFileAnnex());	
			$('#pland').val(goodFileAnnex1());
			$('.dell').unbind("click");
			$('.dell').click(function () {
				$(this).parent().remove();
				$('#planb').val(goodFileAnnex());//键
				$('#pland').val(goodFileAnnex1());//真实名字	
			});
			//alert($('#planb').val());
			//alert($('#pland').val());
			/*
			$('#planb').val(goodFileAnnex());	
			$('#pland').val(goodFileAnnex1());	
			//alert($('#pland').val());	
			$('.dell').unbind("click");
			$('.dell').click(function () {
				$(this).parent().remove();
				$('#planb').val(goodFileAnnex());
				$('#pland').val(goodFileAnnex1());	
			});*/
			//alert(myFileArr.length);
			//getPaList();
	 	}
	});
	$('#add_pro_annex')
		.click(function() {
			$('#dialog-form').dialog('open');
	});
	//结束预案对话框
    getCurrentTime();
    findUserInfo1();
	selectPlanLevel();//选择预案等级
	findEventByID();
	//查询呈批实体
    //findEventPropose();
	
    //findReportTableEnt();
    //alert("dfgdfg");
    $('.addPropose4dbld').click(function(){
    	
    	$("#deptul").empty();
    	$("#selDepts").empty();
		findAllDeptType();
		
				$("#selDepts").empty();
		$("#subDepts").empty();
		if($(this).attr("id")!=undefined&&$(this).attr("id")!="")//判断元素的id是否有值
	{
			var tmp_num=parseInt($(this).attr("id"));
			var tmp_str=parseInt($(this).attr("id")).toString();// 判断是否是角色的ID
			if(tmp_str!="NaN"&&tmp_num>=1000&&tmp_num<=1020)
		{
			default_role=$(this).attr("id");//如果角色是数字的话赋值给d_r
			$("#roleList option[value="+default_role+"]").attr("selected",true);//选中角色
			$.ajax({  //对应 default_role左边框的人员
		         type: "post",
		         url: "admin/queryUser_query.action",
		         data:{
			     "queryOption['URole']": default_role
		         },
		         success: function(data){
		        	 /**
		        	  * 2011年6月26号修改
		        	  */
		        	 showDialogRole(data)
		        	 /**
		        	  * 2011年6月26号修改
		        	  */
		     }
	         });//ajax
		}else{ //如果$(this).attr("id")为string
				$("#roleList option[value='noselect']").attr("selected",true);//选择 “请选择”
			 }
			
	}else{ //如果$(this).attr("id")为空
				$("#roleList option[value='noselect']").attr("selected",true);//选择 “请选择”
		 }
		
		
		
		
		if($('#PComments').val()==""){
			alert("请填写呈批意见！");
			return;
		}/*
		if($('#targetAdmin1').text()==""){
			alert("请选择要呈批目标领导！");
			return;
		}*/
		//
		/*
		if ($("#planPP").children().get(0).checked == true) {
            aud = 1;
        }
        else {
            aud = 2;
        }*/
		//alert($("input[type='radio']:checked").val());
		//alert("ggggggggggggggggggggggggg");
		//$("#notifyLeaderWindow").dialog("open");
        //savePropose($(this));
		ctag="1";
		$("#leaderChooseWindow").dialog("open");
    });
    $('#spanPlanR').click(function(){
        //setEventPlan2Session();
        //alert("dfgdfgdf");
        //$("input[name='planRadio']:checked").checked=false;
        //$('input[@name=planRadio]').get(1).checked = false;
        // var $selectedvalue = $("input[name='planRadio']:checked").val();
        //$('#cacelCheckBox').click(cancelCheckBox);
        $("[name='planRadio']:checked").attr("checked", false);
    });
    /**
     * 开始对话框监听
     */
    $("#dialog").dialog("destroy");
    $("#dialog-form").dialog({
        autoOpen: false,
        width: 460,
        modal: true,
        resizable: false,
        close: function(){
            $("#user-form input").removeClass('invalid');
            $("#user-form label").remove('.invalid');
            getPaList();
        }
    });
    $('#add_propose_annex').click(function(){
        $('#dialog-form').dialog('open');
    });
	/**
	 * 添加处理意见按钮
	 */	
	$('#addPro').toggle(
		function(){
			$('#addPro').val("《《添加处理意见");
			$('#helpBD').show();
		},
		function(){
			$('#addPro').val("添加处理意见》》");
			$('#helpBD').hide();
		}
	);
});


function goodFileAnnex1(){
	var aa=1;
	var fileStr="";
	$('#paListDIV .fn').each(function(i,n){
		//alert(n.tbRole.RName);
		//uRoles[i] = "  "+n.tbRole.RName ;
		//alert($(this).text());
		if(aa==1){
			aa=2;
		}else{
			fileStr+=",";
		}
		fileStr+=$(this).text();
	});
	return fileStr;
}

function goodFileAnnex(){
	var aa=1;
	var fileStr="";
	$('#paListDIV .crtName').each(function(i,n){
		//alert(n.tbRole.RName);
		//uRoles[i] = "  "+n.tbRole.RName ;
		//alert($(this).text());
		if(aa==1){
			aa=2;
		}else{
			fileStr+=",";
		}
		fileStr+=$(this).text();
	});
	return fileStr;
}

/**
 * 作增加呈批 参量rr是目标呈批领导ID
 */
function savePropose(rr){
	//alert($('#planb').val());
	//alert($('#pland').val());
	//alert("gggg454545");
	/*
    var EId = getCookie(eventId);
    var url = 'work2/LZMReport_savePropose.action';
    var params = {
        'proposes.tbUserByPViceCommander.UId': 'ag',
        'proposes.tbUserByPTeamLeader.UId': 'jfry',
        'proposes.tbUserByPCommander.UId': 'zhz',
        'proposes.tbEvent.EId': EId,
		'proposes.c': $('#c').attr('value'),
		//下面是部门
		'proposes.tbUserDeptByPImplementPlan.udId': 'CR0102000000',
		'proposes.tbUserDeptByPAssistDept.udId': 'CR0101000000',
		//下面是呈批记录
		'proposeRecord.PComments': $('#PComments').attr('value')
    };
    jQuery.post(url, params, saveProposeRst, 'json');
    */
	//开始指挥长 ，有1为ID，没有1为名字
	//rr.attr('disabled',"true");
	var a=$('#tbUserByPCommander').text();
	var b=$('#tbUserByPViceCommander').text();
	var c=$('#tbUserByPTeamLeader').text();
	var d=$('#tbUserDeptByPImplementPlan').text();
	var e=$('#tbUserDeptByPAssistDept').text();
	
//	var a=$('#tbUserByPCommander1').text();
//	var b=$('#tbUserByPViceCommander1').text();
//	var c=$('#tbUserByPTeamLeader1').text();
//	var d=$('#tbUserDeptByPImplementPlan').text();
//	var e=$('#tbUserDeptByPAssistDept1').text();
//	alert(a+" "+b+" "+c+" "+d+" "+e);
	var houe=$("input[type='radio']:checked").val();
	//alert(a);
	//结束指挥长
	//alert($('#targetAdmin1').text());
	var tdsp="";
	tdsp+=$('#targetDeptSP').text();
	tdsp+=",";
	tdsp+=$('#targetDeptSP1').text();
	var EId = getCookie(eventId);
    var url = 'work2/LZMReport_savePropose.action';
    var params = {
		'proposeRecord.c': '0',
		'proposeRecord.a': tdsp,
		//'proposeRecord.b': 'dbld',
		//'proposeRecord.tbUserByPrCheckId.UId': 'dbld',
		//'proposeRecord.tbUserByPrCheckId.UId': $('#targetAdmin1').text(),
		'proposeRecord.tbUserByPrCheckId.UId': rr,
        'proposeRecord.PViceCommander': b,
        'proposeRecord.PTeamLeader': c,
        'proposeRecord.PCommander': a,
        'proposes.tbEvent.EId': EId,
		//'proposes.c': $('#c').attr('value'),
		//下面是部门
		'proposeRecord.PImplementPlan': d,
		'proposeRecord.PAssistDept': e,
		//下面是呈批记录
		'proposeRecord.PComments': $('#PComments').attr('value'),
		//下面是预案等级建议
		'proposeRecord.e':houe,
		//下面是附件
		'proposeRecord.f':$('#planb').val(),
		'proposeRecord.g':$('#pland').val()
    };
	//alert($('#planb').val());
	//alert($('#pland').val());
    jQuery.post(url, params, saveProposeRst, 'json');
}

/*
 * @param {Object} data
 */

/**
 * 作增加呈批回调函数
 */
function saveProposeRst(data){
	
	if (data.ajaxRst == "success") {
		//alert("begin");
	saveToFomalPropose9();
	//alert("end");
		window.location.href = "reportTable.jsp?reportTag=succ";
	}else{
		alert("服务器异常！");
	}	
    //
    //alert(data.ajaxRst);
}

function saveToFomalPropose9(){
		var a=$('#tbUserByPCommander1').text();
		var b=$('#tbUserByPViceCommander1').text();
		var c=$('#tbUserByPTeamLeader1').text();
		var d=$('#tbUserDeptByPImplementPlan').text();
		var e=$('#tbUserDeptByPAssistDept1').text();
		
		
		//alert("the same");
		saveAdmin9(a,b,c,d,e);
		//alert(a+" "+b+" "+c+" "+d+" "+e);
	}
	
function saveAdmin9(a,b,c,d,e){
	//alert("gggg454545");
	//alert(b);	
    var EId = getCookie(eventId);
    var url = 'work2/LZMReport_saveAdmin.action';
    var params = {
        'proposes.tbUserByPViceCommander.UId': b,
        'proposes.tbUserByPTeamLeader.UId': c,
        'proposes.tbUserByPCommander.UId': a,
        'proposes.tbEvent.EId': EId,
		//下面是部门
		'proposes.e': d,
		'proposes.tbUserDeptByPAssistDept.udId': e
    };
    jQuery.post(url, params, saveAdminRstZRT9, 'json');
}
	
/**
 * 作增加呈批回调函数
 */
function saveAdminRstZRT9(data){
	if (data.ajaxRst == "success") {
		//alert("OK");
		//window.location.href="reportTable.jsp?reportTag=succ";
		//checkEmergency();
		//setEventPlan2SessionNoPlan();
		//setCookie(BACK_URL,"event_query_start.jsp")
		//window.location.href="operate_succ.jsp";
		//window.location.href="/ag/work/operate_rst_noplan.jsp";
		window.location.href="reportTable.jsp?reportTag=succ";
	}else{
		alert("服务器异常！");
	}	
    //
    //alert(data.ajaxRst);
}

/**
 * 查询事件实体
 */
function findEventByID(){
	//alert("dfgfd");
    var timestamp = Date.parse(new Date());
    $.get("work2/LZMReport_findEventByID.action?ts=" + timestamp + "&EId=" + getCookie(eventId), null, findEventByIDRst);
}

/**
 * 查询事件回调函数
 */
function findEventByIDRst(data){
    if (data.ajaxRst == "success") {
		$('#EEventname').text(data.event.EEventname);
        $('#EId').text(data.event.EId);
		$('#tbesEventStatus').text(data.event.tbEventStatus.tbesEventStatus);
        $('#EEventCause').text(data.event.EEventCause);
        $('#EEventDesc').text(data.event.EEventDesc);
		alert(data.event.tbEventLevel.elEventLevel);
        $('#tbesEventLevel').text(data.event.tbEventLevel.elEventLevel);
        var ETakeMeasure=data.event.ETakeMeasure;
		if(ETakeMeasure==null)
		ETakeMeasure="暂无";
        $('#ETakeMeasure').text(ETakeMeasure);
        $('#tbEventSource').text(data.event.tbEventSource.esEventSource);
    }
    else {
        alert("查询事件失败，请重新登陆 ！");
    }
}

/**
 * 查询呈批实体
 */
function findEventPropose(){
	//alert("findEventPropose");
    var timestamp = Date.parse(new Date());
    $.get("work2/LZMReport_findEventPropose.action?ts=" + timestamp + "&EId=" + getCookie(eventId), null, findEventProposeRst);
}

/**
 * 查询呈批实体回调函数
 */
function findEventProposeRst(data){
    //alert("fdgfdgfdg");
    if (data.ajaxRst == "success") {
        //$('#adminSP').text(data.tbUser.UName);
        //alert("7978798");
        //$('#EEventCause').val(data.proposesList[0].tbEvent.EEventCause);
        //$('#EEventCause').val(data.proposes.tbEvent.EEventCause);
		
		$('#EEventname').text(data.proposes.tbEvent.EEventname);
        $('#EId').text(data.proposes.tbEvent.EId);
		
		
        $('#EEventCause').val(data.proposes.tbEvent.EEventCause);
        $('#EEventDesc').val(data.proposes.tbEvent.EEventDesc);
        $('#ETakeMeasure').val(data.proposes.tbEvent.ETakeMeasure);
        $('#tbEventSource').text(data.proposes.tbEvent.tbEventSource.esEventSource);
        /*
         $('#tbUserByPCommander').text(data.proposes.tbUserByPCommander.UName);
         $('#tbUserByPViceCommander').text(data.proposes.tbUserByPViceCommander.UName);
         $('#tbUserByPTeamLeader').text(data.proposes.tbUserByPTeamLeader.UName);
         $('#tbUserDeptByPImplementPlan').text(data.proposes.tbUserDeptByPImplementPlan.udName);
         $('#tbUserDeptByPAssistDept').text(data.proposes.tbUserDeptByPAssistDept.udName);*/
        //$('#c').text(data.proposes.c);
    }
    else {
        alert("服务器异常，请检查后重试 ！");
    }
}

function cancelCheckBox(){
    $("[name='planRadio']:checked").attr("checked", false);
}

function getCurrentTime(){
    var Timer = new Date();
    var years = Timer.getFullYear();
    var months = Timer.getMonth() + 1;
    var days = Timer.getDate();
    var hours = Timer.getHours();
    var minutes = Timer.getMinutes();
    var seconds = Timer.getSeconds();
    if (minutes <= 9) 
        minutes = "0" + minutes;
    if (seconds <= 9) 
        seconds = "0" + seconds;
    /*
     var dateTime = years + "-" + months + "-" + days + " " + hours
     + ":" + minutes + ":" + seconds;*/
    var dateTime = years + "-" + months + "-" + days;
    $("#dateTime").text(dateTime);
}

function findUserInfo(){
    var timestamp = Date.parse(new Date());
    $.get("admin/UserServ_findUserInfo.action?ts=" + timestamp, null, findUserInfoRst);
}

function findUserInfoRst(data){
    if (data.ajaxRst == "success") {
        $('#adminSP').text(data.tbUser.UName);
    }
}

function findUserInfo1(){
    var timestamp = Date.parse(new Date());
    $.get("admin/UserServ_findUserInfo.action?ts=" + timestamp, null, findUserInfoRst1);
}

function findUserInfoRst1(data){
    if (data.ajaxRst == "success") {
        //var roleList=data.tbUser.tbUserRoles[0].tbRole.RName;
        //$('#roleSP').text(roleList);
        //var roleStr="";
        //$.each(data.tbUser.tbUserRoles,function(i,n){
        //fillDatatoHTML(n.PId,n.PName,n.tbPlanGrade.pgName,n.tbPlanStatus.pstName,n.tbPlanType.ptName);
        //alert(n.tbRole.RName);
        //roleStr+=n.tbRole.RName;
        //roleStr+="、";
        //});
        //alert(data.tbUser.tbUserRoles.length);
        //alert(data.tbUser.ULoginTime);
        $('#adminSP').text(data.tbUser.UName);
        $('#adminSP1').text(data.tbUser.UName);
        $('#loginSP').text(data.tbUser.ULoginTime.replace(/T/, " "));
        var uRoles = new Array(data.tbUser.tbUserRoles.length);
		//setCookie("crtRole",data.tbUser.tbUserRoles[0]);
		//alert(data.tbUser.tbUserRoles[0].tbRole.RId+"22");
		var goo;
		goo=parseInt(data.tbUser.tbUserRoles[0].tbRole.RId);
		if(goo==1002){			
			findAllRole(rolechooser,1004);//多角色格式如下：var rolechooser = "1001,1002";
		}else if(goo==1004){			
			findAllRole(rolechooser,1005);//多角色格式如下：var rolechooser = "1001,1002";
		}else{
			findAllRole(rolechooser,goo+1);//多角色格式如下：var rolechooser = "1001,1002";
		}
        $.each(data.tbUser.tbUserRoles, function(i, n){
            //alert(n.tbRole.RId);
            uRoles[i] = "  " + n.tbRole.RName;
        });
        var strhtml = "";
        strhtml += uRoles;
        $('#roleSP').text(strhtml);
        /**
         * 下面是显示部门的信息
         */
        //alert(data.tbUser.tbUserDepts.length);
        var uDepts = new Array(data.tbUser.tbUserDepts.length);
        $.each(data.tbUser.tbUserDepts, function(i, n){
            //alert(n.tbUserDepts.udName);
            uDepts[i] = "  " + n.udName;
        });
        var strhtml1 = "";
        strhtml1 += uDepts;
        $('#deptSP').text(strhtml1);
        $('#deptSP1').text(strhtml1+"意见");
    }
}



